//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ehl.capture.tool;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;

public final class StringHelper {
    public StringHelper() {
    }

    public static final int obj2int(Object o, int defaultValue) {
        if (o instanceof Number) {
            return ((Number)o).intValue();
        } else {
            if (o instanceof String) {
                String s = ((String)o).trim();

                try {
                    return Integer.parseInt(s);
                } catch (Exception var5) {
                    try {
                        return Double.valueOf(s).intValue();
                    } catch (Exception var4) {
                        ;
                    }
                }
            }

            return defaultValue;
        }
    }

    public static final long obj2long(Object o, long defaultValue) {
        if (o instanceof Number) {
            return ((Number)o).longValue();
        } else {
            if (o instanceof String) {
                String s = ((String)o).trim();

                try {
                    return Long.parseLong(s);
                } catch (Exception var6) {
                    try {
                        return Double.valueOf(s).longValue();
                    } catch (Exception var5) {
                        ;
                    }
                }
            }

            return defaultValue;
        }
    }

    public static final String numToFixedLengthStr(Number num, int fixedLen, char c) {
        return numToFixedLengthStr(num, fixedLen, c, false);
    }

    public static final String numToFixedLengthStr(Number num, int fixedLen, char c, boolean bsuffix) {
        String finiStr = String.valueOf(num.longValue());
        if (finiStr == null) {
            return null;
        } else {
            if ((finiStr = finiStr.trim()).length() > fixedLen) {
                if (bsuffix) {
                    finiStr = finiStr.substring(finiStr.length() - fixedLen);
                } else {
                    finiStr = finiStr.substring(0, fixedLen);
                }
            } else {
                while(finiStr.length() < fixedLen) {
                    finiStr = c + finiStr;
                }
            }

            return finiStr;
        }
    }

    public static final boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }

    public static final boolean isNotEmpty(String s) {
        return s != null && s.trim().length() > 0;
    }

    public static final String obj2str(Object text) {
        return text != null ? text.toString() : null;
    }

    public static final String obj2str(Object text, String dftValue) {
        return text != null ? text.toString() : dftValue;
    }

    public static final String obj2strEmpty(Object text) {
        return text == null ? "" : text.toString();
    }

    private static boolean isNotChar(String s, char c) {
        int i = s.length();

        for(int j = 0; j < i; ++j) {
            if (s.charAt(j) == c) {
                return false;
            }
        }

        return true;
    }

    public static final boolean isValueLegal(String s, String compareStr) {
        for(int j = 0; j < compareStr.length(); ++j) {
            if (!isNotChar(s, compareStr.charAt(j))) {
                return false;
            }
        }

        return true;
    }

    public static final String[] split(String src, String delimer) {
        int iSrcLen = src.length();
        int iDlmLen = delimer.length();
        ArrayList arraylist = new ArrayList();
        int pos;
        for(int k = 0; k < iSrcLen; k = pos + iDlmLen) {
            if ((pos = src.indexOf(delimer, k)) < 0) {
                arraylist.add(src.substring(k));
                break;
            }

            arraylist.add(src.substring(k, pos));
        }

        String[] as = new String[arraylist.size()];
        return (String[])arraylist.toArray(as);
    }

    public static final String toValidString(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static final String validate(String s) {
        return s == null ? "" : s.trim();
    }

    public static final String translate(String s, String charSetFrom, String charSetTo) throws UnsupportedEncodingException {
        if (s == null) {
            return null;
        } else {
            byte[] b;
            if (charSetFrom != null && charSetFrom.trim().length() == 0) {
                b = s.getBytes(charSetFrom);
            } else {
                b = s.getBytes();
            }

            return new String(b, charSetTo);
        }
    }

    public static final String translateFromISO(String s, String charSetTo) throws UnsupportedEncodingException {
        return translate(s, "ISO8859-1", charSetTo);
    }

    public static final String translateToISO(String s) throws UnsupportedEncodingException {
        return translate(s, (String)null, "ISO8859-1");
    }

    public static final boolean isNumber(String value) {
        return value.matches("[0-9]*");
    }

    public static String[] getSplitValue(String str) {
        if (str != null && str.trim().length() != 0) {
            String[] a = split(str, ":");
            String p = a != null && a.length > 0 ? a[0] : null;
            String pb1 = null;
            String pb2 = null;
            String[] rtn = null;
            if (p != null && p.trim().length() > 0) {
                String[] b = split(p, "-");
                pb1 = b != null && b.length > 0 ? b[0] : null;
                pb2 = b != null && b.length > 0 ? b[1] : null;
            }

            if (pb1 != null && pb2 != null) {
                rtn = new String[]{pb1, pb2};
            }

            return rtn;
        } else {
            return null;
        }
    }

    public static double numberRound(double dx, int y) {
        String formatstr = "################";
        if (y != 0) {
            formatstr = formatstr + ".";

            for(int i = 0; i < y; ++i) {
                formatstr = formatstr + "#";
            }
        }

        String str = (new DecimalFormat(formatstr)).format(dx);
        return Double.parseDouble(str);
    }
}
